//
//  XGCalendarHeaderView.m
//  SACalendar
//
//  Created by YaoZhaofeng on 15/4/17.
//  Copyright (c) 2015年 SyncoApp. All rights reserved.
//

#import "XGCalendarHeaderView.h"
#import "XGCalendarConstants.h"

@interface XGCalendarHeaderView ()
@property (nonatomic, strong) UILabel *monthTitle;
@property (nonatomic, strong) NSMutableArray *columnTitles;
@property (nonatomic, strong) NSMutableArray *columnLabels;

@property (nonatomic, strong) UIButton *forwardButton;
@property (nonatomic, strong) UIButton *backwardButton;
@end

@implementation XGCalendarHeaderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:kCalendarColorHeaderGradientDark];
        
        _monthTitle = [UILabel new];
        [_monthTitle setTextColor:kCalendarColorHeaderMonth];
        [_monthTitle setBackgroundColor:[UIColor clearColor]];
        [_monthTitle setTextAlignment:NSTextAlignmentCenter];
        [_monthTitle setFont:[UIFont systemFontOfSize:20]];
        [self addSubview:_monthTitle];
        
        _columnTitles = [NSMutableArray arrayWithArray:@[@"日",@"一",@"二",@"三",@"四",@"五",@"六"]];
        _columnLabels = [NSMutableArray new];
        
        self.backwardButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        _backwardButton.backgroundColor = [UIColor clearColor];
        [_backwardButton addTarget:self action:@selector(backwardButtonTapped) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.backwardButton];
        
        self.forwardButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width - 50, 0, 50, 50)];
        _forwardButton.backgroundColor = [UIColor clearColor];
        [_forwardButton addTarget:self action:@selector(forwardButtonTapped) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.forwardButton];
        
        CGFloat labelWidth = [self frame].size.width/7;
        CGFloat labelHeight = 20;
        
        for (NSUInteger i = 0; i < [[self columnTitles] count]; i++) {
            NSString *title = [self columnTitles][i];
            
            UILabel *label = [self _columnLabelWithTitle:title];
            label.tag = i;
            [[self columnLabels] addObject:label];
            
            CGRect frame = CGRectMake(i*labelWidth, [self frame].size.height-labelHeight, labelWidth, labelHeight);
            [label setFrame:frame];
            
            [self addSubview:label];
        }
    }
    return self;
}

- (void)setHeaderTitle:(NSString*)title{
    [_monthTitle setText:title];
}

/* Creates and configures a label for a column title */

- (UILabel *)_columnLabelWithTitle:(NSString *)title
{
    UILabel *l = [UILabel new];
    [l setBackgroundColor:[UIColor clearColor]];
    if ([title isEqualToString:@"日"] || [title isEqualToString:@"六"]) {
        [l setTextColor:kCalendarColorHeaderweekendTitle];
    }else{
        [l setTextColor:kCalendarColorHeaderWeekdayTitle];
    }
    [l setTextAlignment:NSTextAlignmentCenter];
    [l setFont:[UIFont boldSystemFontOfSize:11]];
    [l setText:title];
    
    return l;
}

#pragma mark - Button Handling

- (void)forwardButtonTapped
{
    if ([[self delegate] respondsToSelector:@selector(forwardTapped)]) {
        [[self delegate] forwardTapped];
    }
}

- (void)backwardButtonTapped
{
    if ([[self delegate] respondsToSelector:@selector(backwardTapped)]) {
        [[self delegate] backwardTapped];
    }
}

- (void)layoutSubviews
{
    CGFloat yOffset = 13.0f;
    CGFloat titleLabelHeight = 40.0f;
    
    CGRect frame = CGRectMake(0, yOffset, [self frame].size.width, titleLabelHeight);
    [[self monthTitle] setFrame:frame];
    
    /* Update the month title. */
    
    if (_dataSource &&[_dataSource respondsToSelector:@selector(titleForHeader:)]) {
        NSString *title = [[self dataSource] titleForHeader:self];
        [[self monthTitle] setText:title];
    }
    
    /* Show the forward and back buttons */
    //TODO:
    
    
    //resize column labels
    CGFloat labelWidth = [self frame].size.width/7;
    CGFloat labelHeight = 20;
    
    for (UILabel *label in [self columnLabels]) {
        int i = (int)label.tag;
        CGRect frame = CGRectMake(i*labelWidth, [self frame].size.height-labelHeight, labelWidth, labelHeight);
        [label setFrame:frame];
    }
}


@end
